package mahongyuan.com.studentsystem;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import reflash.ApkEntity;

import menuview.slidingmenu;

public class MainActivity extends FragmentActivity implements View.OnClickListener{
    ArrayList<ApkEntity> apk_list;
    private slidingmenu mLeftMenu ;

//    //声明ViewPager
//    private ViewPager viewPager;
//    //声明ViewPager的适配器
//    private FragmentPagerAdapter mAdapter;

    private List<Fragment> mFragments;

    private LinearLayout mTabHome;
    private LinearLayout mTabMajor;
    private LinearLayout mTabStudent;
    private LinearLayout mTabCollage;

    //声明四个Tab分别对应的Fragment
    private Fragment mFragWeinxin;
    private Fragment mFragFrd;
    private Fragment mFragAddress;
    private Fragment mFragSetting;

    private RelativeLayout mItem;
    private TextView textView;

    private ImageButton mHomeImg;
    private ImageButton mMajorImg;
    private ImageButton mStudentImg;
    private ImageButton mCollageImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();//初始化控件
        initEvent();//初始化事件
        initData();//初始化数据
        setTab(0);
    }
    //侧滑菜单功能
    public void toggleMenu(View view)
    {
        mLeftMenu.toggle();
    }
    /*
    * 初始化控件
    * */
    private void initView() {
//        viewPager = (ViewPager)findViewById(R.id.viewpager);

        mTabHome = (LinearLayout)findViewById(R.id.id_tab_home);
        mTabMajor = (LinearLayout)findViewById(R.id.id_tab_student);
        mTabStudent = (LinearLayout)findViewById(R.id.id_tab_major);
        mTabCollage = (LinearLayout)findViewById(R.id.id_tab_collage);

        mHomeImg = (ImageButton)findViewById(R.id.id_tab_home_btn);
        mMajorImg = (ImageButton)findViewById(R.id.id_tab_major_btn);
        mStudentImg = (ImageButton)findViewById(R.id.id_tab_student_btn);
        mCollageImg = (ImageButton)findViewById(R.id.id_tab_collage_btn);

        mItem = (RelativeLayout)findViewById(R.id.item4);
        textView = (TextView)findViewById(R.id.text);
        mLeftMenu = (slidingmenu) findViewById(R.id.id_menu);

    }

    private void initEvent() {
        // 设置事件
        mTabStudent.setOnClickListener(this);
        mTabMajor.setOnClickListener(this);
        mTabCollage.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
        mItem.setOnClickListener(this);
    }

    private void initData() {

        mFragments = new ArrayList<Fragment>();
        Fragment mTab01 = new HomeFragment();
        Fragment mTab02 = new MajorFragment();
        Fragment mTab03 = new ClassFragment();
        Fragment mTab04 = new CollageFragment();
        mFragments.add(mTab01);
        mFragments.add(mTab02);
        mFragments.add(mTab03);
        mFragments.add(mTab04);

//        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
//        {
//            @Override
//            public int getCount()
//            {
//                return mFragments.size();
//            }
//            @Override
//            public Fragment getItem(int arg0)
//            {
//                return mFragments.get(arg0);
//            }
//        };
//        viewPager.setAdapter(mAdapter);
//        //添加ViewPager的切换Tab的监听事件
//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            //ViewPager 改变时改变图标的颜色
//            @Override
//            public void onPageSelected(int arg0) {
//                //获取ViewPager的当前Tab
//                int currentItem = viewPager.getCurrentItem();
//                resetImg();
//                setTab(currentItem);
//            }
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//            }
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//
//            }
//        });
    }
    @Override
    public void onClick(View v) {
        //先将四个ImageButton置为灰色
        resetImg();
        switch (v.getId()) {
            case R.id.id_tab_home:
                setTab(0);
                break;
            case R.id.id_tab_major:
                setTab(1);
                break;
            case R.id.id_tab_student:
                setTab(2);
//                setData();
//                showList(apk_list);
                break;
            case R.id.id_tab_collage:
                setTab(3);
                break;
            case R.id.item4:
                Intent intent = new Intent(MainActivity.this,Setting.class);
                startActivity(intent);
            default:
                break;
        }

    }

    private void setTab(int i)
    {
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            case 0:
                mHomeImg.setImageResource(R.mipmap.icon_blue_1);
                textView.setText("主页");
                //如果微信对应的Fragment没有实例化，则进行实例化，并显示出来
                if (mFragWeinxin == null) {
                    mFragWeinxin = new HomeFragment();
                    transaction.add(R.id.id_content, mFragWeinxin);
                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    transaction.show(mFragWeinxin);
                }
                break;
            case 1:
                mMajorImg.setImageResource(R.mipmap.icon_blue_2);
                textView.setText("专业");
                if (mFragFrd == null) {
                    mFragFrd = new MajorFragment();
                    transaction.add(R.id.id_content, mFragFrd);
                } else {
                    transaction.show(mFragFrd);
                }
                break;
            case 2:
                mStudentImg.setImageResource(R.mipmap.icon_blue_3);
                textView.setText("学生");
                if (mFragAddress == null) {
                    mFragAddress = new ClassFragment();
                    transaction.add(R.id.id_content, mFragAddress);
                } else {
                    transaction.show(mFragAddress);
                }
                break;
            case 3:
                mCollageImg.setImageResource(R.mipmap.icon_blue_4);
                textView.setText("学院");
                if (mFragSetting == null) {
                    mFragSetting = new CollageFragment();
                    transaction.add(R.id.id_content, mFragSetting);
                } else {
                    transaction.show(mFragSetting);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
//        viewPager.setCurrentItem(i);
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (mFragWeinxin != null) {
            transaction.hide(mFragWeinxin);
        }
        if (mFragFrd != null) {
            transaction.hide(mFragFrd);
        }
        if (mFragAddress != null) {
            transaction.hide(mFragAddress);
        }
        if (mFragSetting != null) {
            transaction.hide(mFragSetting);
        }
    }
    //将所有的图片都变暗
    private void resetImg(){
        mHomeImg.setImageResource(R.mipmap.icon_1);
        mMajorImg.setImageResource(R.mipmap.icon_2);
        mStudentImg.setImageResource(R.mipmap.icon_3);
        mCollageImg.setImageResource(R.mipmap.icon_4);
    }


}



